import axios from 'axios';
// import router from '../routerManuaConfig'
import router from '../router/index'
import store from "../store";
import Vue from 'vue';
let base = '';
// let base = 'http://10.27.7.217:8082';//http://localhost:8081     http://139.129.220.20:8082
// 如果是IIS部署，用这个，因为 IIS 只能是 CORS 跨域，不能代理
// let base = process.env.NODE_ENV=="production"? 'http://localhost:8081':'';


// 请求延时
axios.defaults.timeout = 20000

var storeTemp = store;
axios.interceptors.request.use(
    config => {
        var curTime = new Date()
        var expiretime = new Date(Date.parse(storeTemp.state.tokenExpire))

        if (storeTemp.state.token && (curTime < expiretime && storeTemp.state.tokenExpire)) {
            // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers.Authorization = "Bearer " + storeTemp.state.token;
        }

        saveRefreshtime();

        return config;
    },
    err => {
        return Promise.reject(err);
    }
);

// http response 拦截器
axios.interceptors.response.use(


    response => {
        return response;
    },
    error => {

        console.log('axios.interceptors.respons');


        // 超时请求处理
        var originalRequest = error.config;
        if (error.code == 'ECONNABORTED' && error.message.indexOf('timeout') != -1 && !originalRequest._retry) {

            Vue.prototype.$message({
                message: '请求超时！',
                type: 'error'
            });

            originalRequest._retry = true
            return null;
        }

        if (error.response) {
            if (error.response.status == 401) {
                var curTime = new Date()
                var refreshtime = new Date(Date.parse(window.localStorage.refreshtime))
                // 在用户操作的活跃期内
                if (window.localStorage.refreshtime && (curTime <= refreshtime)) {
                    return refreshToken({ token: window.localStorage.Token }).then((res) => {
                        if (res.success) {
                            Vue.prototype.$message({
                                message: 'refreshToken success! loading data...',
                                type: 'success'
                            });

                            store.commit("saveToken", res.token);

                            var curTime = new Date();
                            var expiredate = new Date(curTime.setSeconds(curTime.getSeconds() + res.expires_in));
                            store.commit("saveTokenExpire", expiredate);

                            error.config.__isRetryRequest = true;
                            error.config.headers.Authorization = 'Bearer ' + res.token;
                            return axios(error.config);
                        } else {
                            // 刷新token失败 清除token信息并跳转到登录页面
                            ToLogin()
                        }
                    });
                } else {
                    // 返回 401，并且不知用户操作活跃期内 清除token信息并跳转到登录页面
                    ToLogin()
                }
            }
            // 403 无权限
            if (error.response.status == 403) {
                Vue.prototype.$message({
                    message: '失败！该操作无权限',
                    type: 'error'
                });
                return null;
            }
        }
        return ""; // 返回接口返回的错误信息
    }
);


export const BaseApiUrl = base;

// 登录
export const requestLogin = params => {
    return axios.get(`${base}/api/login/jwttoken3.0`, { params: params }).then(res => res.data);
};
export const requestLoginMock = params => { return axios.post(`${base}/login`, params).then(res => res.data); };

export const refreshToken = params => {
    return axios.get(`${base}/api/login/RefreshToken`, { params: params }).then(res => res.data);
};

export const saveRefreshtime = params => {

    let nowtime = new Date();
    let lastRefreshtime = window.localStorage.refreshtime ? new Date(window.localStorage.refreshtime) : new Date(-1);
    let expiretime = new Date(Date.parse(window.localStorage.TokenExpire))

    let refreshCount = 1;//滑动系数
    if (lastRefreshtime >= nowtime) {
        lastRefreshtime = nowtime > expiretime ? nowtime : expiretime;
        lastRefreshtime.setMinutes(lastRefreshtime.getMinutes() + refreshCount);
        window.localStorage.refreshtime = lastRefreshtime;
    } else {
        window.localStorage.refreshtime = new Date(-1);
    }
};
const ToLogin = params => {
    store.commit("saveToken", "");
    store.commit("saveTokenExpire", "");
    store.commit("saveTagsData", "");
    window.localStorage.removeItem('user');
    window.localStorage.removeItem('NavigationBar');

    router.replace({
        path: "/login",
        query: { redirect: router.currentRoute.fullPath }
    });

    window.location.reload()

};

// 获取用户详情
export const getUserByToken = params => {
    return axios.get(`${base}/api/user/getInfoByToken`, { params: params }).then(res => res.data);
};


export function testapi2() {
    console.log('api is ok.')
}

export const testapi = pa => {
    console.log('api is ok.')
}
//---------------------------------------------------


// 
export const getPage_CaiJiQiModelValue = params => {
    return axios.get(`${base}/api/CaiJiQiModelValue/GetPageList`, { params: params });
};

export const getByID_CaiJiQiModelValue = params => {
    return axios.get(`${base}/api/CaiJiQiModelValue/Get`, { params: params });
};

export const edit_CaiJiQiModelValue = params => {
    return axios.put(`${base}/api/CaiJiQiModelValue/Update`, params);
};

export const add_CaiJiQiModelValue = params => {
    return axios.post(`${base}/api/CaiJiQiModelValue/Add`, params);
};


export const batchRemove_CaiJiQiModelValue = params => {
    return axios.delete(`${base}/api/CaiJiQiModelValue/BatchDelete`, { params: params });
};

export const Remove_CaiJiQiModelValue = params => {
    return axios.delete(`${base}/api/CaiJiQiModelValue/delete`, { params: params });
};



// 
export const getPage_MQXT_CaiJiQiModel = params => {
    return axios.get(`${base}/api/MQXT_CaiJiQiModel/GetPageList`, { params: params });
};

export const getByID_MQXT_CaiJiQiModel = params => {
    return axios.get(`${base}/api/MQXT_CaiJiQiModel/Get`, { params: params });
};

export const edit_MQXT_CaiJiQiModel = params => {
    return axios.put(`${base}/api/MQXT_CaiJiQiModel/Update`, params);
};

export const add_MQXT_CaiJiQiModel = params => {
    return axios.post(`${base}/api/MQXT_CaiJiQiModel/Add`, params);
};


export const batchRemove_MQXT_CaiJiQiModel = params => {
    return axios.delete(`${base}/api/MQXT_CaiJiQiModel/BatchDelete`, { params: params });
};

export const Remove_MQXT_CaiJiQiModel = params => {
    return axios.delete(`${base}/api/MQXT_CaiJiQiModel/delete`, { params: params });
};



// 
export const getPage_MQXT_DataShowModel = params => {
    return axios.get(`${base}/api/MQXT_DataShowModel/GetPageList`, { params: params });
};

export const getByID_MQXT_DataShowModel = params => {
    return axios.get(`${base}/api/MQXT_DataShowModel/Get`, { params: params });
};

export const edit_MQXT_DataShowModel = params => {
    return axios.put(`${base}/api/MQXT_DataShowModel/Update`, params);
};

export const add_MQXT_DataShowModel = params => {
    return axios.post(`${base}/api/MQXT_DataShowModel/Add`, params);
};


export const batchRemove_MQXT_DataShowModel = params => {
    return axios.delete(`${base}/api/MQXT_DataShowModel/BatchDelete`, { params: params });
};

export const Remove_MQXT_DataShowModel = params => {
    return axios.delete(`${base}/api/MQXT_DataShowModel/delete`, { params: params });
};

// 
export const getPage_MQXT_JiZhongQiModel = params => {
    return axios.get(`${base}/api/MQXT_JiZhongQiModel/GetPageList`, { params: params });
};

export const getByID_MQXT_JiZhongQiModel = params => {
    return axios.get(`${base}/api/MQXT_JiZhongQiModel/Get`, { params: params });
};

export const edit_MQXT_JiZhongQiModel = params => {
    return axios.put(`${base}/api/MQXT_JiZhongQiModel/Update`, params);
};

export const add_MQXT_JiZhongQiModel = params => {
    return axios.post(`${base}/api/MQXT_JiZhongQiModel/Add`, params);
};


export const batchRemove_MQXT_JiZhongQiModel = params => {
    return axios.delete(`${base}/api/MQXT_JiZhongQiModel/BatchDelete`, { params: params });
};

export const Remove_MQXT_JiZhongQiModel = params => {
    return axios.delete(`${base}/api/MQXT_JiZhongQiModel/delete`, { params: params });
};




//---------------------------------------------------
// 用户管理
export const getUserListPage = params => {
    return axios.get(`${base}/api/user/get`, { params: params });
};
export const removeUser = params => {
    return axios.delete(`${base}/api/user/delete`, { params: params });
};
export const editUser = params => {
    return axios.put(`${base}/api/user/put`, params);
};
export const addUser = params => {
    return axios.post(`${base}/api/user/post`, params);
};
export const batchRemoveUser = params => {
    return axios.delete(`${base}/api/Claims/BatchDelete`, { params: params });//没做
};


/// 密码重置
export const UserResetPwd = params => {
    return axios.get(`${base}/api/user/ResetPwd`, { params: params });
};


// 角色管理
export const getRoleListPage = params => {
    return axios.get(`${base}/api/role/get`, { params: params });
};
export const removeRole = params => {
    return axios.delete(`${base}/api/role/delete`, { params: params });
};
export const editRole = params => {
    return axios.put(`${base}/api/role/put`, params);
};
export const addRole = params => {
    return axios.post(`${base}/api/role/post`, params);
};

// 接口模块管理
export const getModuleListPage = params => {
    return axios.get(`${base}/api/module/get`, { params: params });
};
export const removeModule = params => {
    return axios.delete(`${base}/api/module/delete`, { params: params });
};
export const editModule = params => {
    return axios.put(`${base}/api/module/put`, params);
};
export const addModule = params => {
    return axios.post(`${base}/api/module/post`, params);
};


// 菜单模块管理
export const getPermissionListPage = params => {
    return axios.get(`${base}/api/permission/get`, { params: params });
};
export const getPermissionTreeTable = params => {
    return axios.get(`${base}/api/permission/GetTreeTable`, { params: params });
};
export const removePermission = params => {
    return axios.delete(`${base}/api/permission/delete`, { params: params });
};
export const editPermission = params => {
    return axios.put(`${base}/api/permission/put`, params);
};
export const addPermission = params => {
    return axios.post(`${base}/api/permission/post`, params);
};
export const getPermissionTree = params => {
    return axios.get(`${base}/api/permission/getpermissiontree`, { params: params });
};
export const getPermissionIds = params => {
    return axios.get(`${base}/api/permission/GetPermissionIdByRoleId`, { params: params });
};

export const addRolePermission = params => {
    return axios.post(`${base}/api/permission/Assign`, params);
};
// 后台菜单接口
export const getNavigationBar = params => {
    return axios.get(`${base}/api/permission/GetNavigationBar`, { params: params }).then(res => res.data);
};

// Bug模块管理
export const getBugListPage = params => {
    return axios.get(`${base}/api/TopicDetail/get`, { params: params });
};
export const removeBug = params => {
    return axios.delete(`${base}/api/TopicDetail/delete`, { params: params });
};
export const editBug = params => {
    return axios.put(`${base}/api/TopicDetail/update`, params);
};
export const addBug = params => {
    return axios.post(`${base}/api/TopicDetail/post`, params);
};



// 日志
export const getLogs = params => {
    return axios.get(`${base}/api/Monitor/get`, { params: params });
};
export const getRequestApiinfoByWeek = params => {
    return axios.get(`${base}/api/Monitor/GetRequestApiinfoByWeek`, { params: params });
};
export const getAccessApiByDate = params => {
    return axios.get(`${base}/api/Monitor/GetAccessApiByDate`, { params: params });
};
export const getAccessApiByHour = params => {
    return axios.get(`${base}/api/Monitor/GetAccessApiByHour`, { params: params });
};



// 系统配置
// testapi, getUserListPage,getRoleListPage , removeUser, batchRemoveUser, editUser, addUser

export const getConfigListPage = params => {
    return axios.get(`${base}/api/SysConfig/GetPageList`, { params: params });
};

export const getConfigGetByID = params => {
    return axios.get(`${base}/api/SysConfig/Get`, { params: params });
};

export const editConfig = params => {
    return axios.put(`${base}/api/SysConfig/Update`, params);
};

export const addConfig = params => {
    return axios.post(`${base}/api/SysConfig/Add`, params);
};


export const moveConfigre = params => {
    return axios.delete(`${base}/api/SysConfig/delete`, { params: params });
};

export const batchRemoveMove = params => {
    return axios.delete(`${base}/api/SysConfig/BatchDelete`, { params: params });//没做
};


//  数据字典

export const getSys_DictPage = params => {
    return axios.get(`${base}/api/Sys_Dict/GetPageList`, { params: params });
};

export const getSys_DictGetByID = params => {
    return axios.get(`${base}/api/Sys_Dict/Get`, { params: params });
};

export const editSys_Dict = params => {
    return axios.put(`${base}/api/Sys_Dict/Update`, params);
};

export const addSys_Dict = params => {
    return axios.post(`${base}/api/Sys_Dict/Add`, params);
};


export const batchRemoveSys_Dict = params => {
    return axios.delete(`${base}/api/Sys_Dict/delete`, { params: params });
};

export const RemoveSys_Dict = params => {
    return axios.delete(`${base}/api/Sys_Dict/BatchDelete`, { params: params });//没做
};






 


// 
export const getPage_Sys_Dept = params => {
    return axios.get(`${base}/api/Sys_Dept/GetPageList`, { params: params });
};

export const getByID_Sys_Dept = params => {
    return axios.get(`${base}/api/Sys_Dept/Get`, { params: params });
};

export const edit_Sys_Dept = params => {
    return axios.put(`${base}/api/Sys_Dept/Update`, params);
};

export const add_Sys_Dept = params => {
    return axios.post(`${base}/api/Sys_Dept/Add`, params);
};


export const batchRemove_Sys_Dept = params => {
    return axios.delete(`${base}/api/Sys_Dept/BatchDelete`, { params: params });
};

export const Remove_Sys_Dept = params => {
    return axios.delete(`${base}/api/Sys_Dept/delete`, { params: params });
};



export const GetList_Sys_Dept = params => {
    return axios.get(`${base}/api/Sys_Dept/GetList`, { params: params });
};


//================================================     效率通     ====================================================================



export const GetListToUI_WY_Company = params => {
    return axios.get(`${base}/api/WY_Company/GetListToUI`, { params: params });
};
// 
export const getList_WY_Company = params => {
    return axios.get(`${base}/api/WY_Company/GetList`, { params: params });
};

export const getPage_WY_Company = params => {
    return axios.get(`${base}/api/WY_Company/GetPageList`, { params: params });
};

export const getByID_WY_Company = params => {
    return axios.get(`${base}/api/WY_Company/Get`, { params: params });
};

export const edit_WY_Company = params => {
    return axios.put(`${base}/api/WY_Company/Update`, params);
};

export const add_WY_Company = params => {
    return axios.post(`${base}/api/WY_Company/Add`, params);
};


export const batchRemove_WY_Company = params => {
    return axios.delete(`${base}/api/WY_Company/BatchDelete`, { params: params });
};

export const Remove_WY_Company = params => {
    return axios.delete(`${base}/api/WY_Company/delete`, { params: params });
};



export const GetTreeComPro_WY_Project = params => {
    return axios.get(`${base}/api/WY_Company/GetTreeComPro`, { params: params });
};




// 
export const getPage_WY_Project = params => {
    return axios.get(`${base}/api/WY_Project/GetPageList`, { params: params });
};

export const getByID_WY_Project = params => {
    return axios.get(`${base}/api/WY_Project/Get`, { params: params });
};

export const edit_WY_Project = params => {
    return axios.put(`${base}/api/WY_Project/Update`, params);
};

export const add_WY_Project = params => {
    return axios.post(`${base}/api/WY_Project/Add`, params);
};


export const batchRemove_WY_Project = params => {
    return axios.delete(`${base}/api/WY_Project/BatchDelete`, { params: params });
};

export const Remove_WY_Project = params => {
    return axios.delete(`${base}/api/WY_Project/delete`, { params: params });
};




// 设备类型表
export const getPage_Base_EquipmentCategory = params => {
    return axios.get(`${base}/api/Base_EquipmentCategory/GetPageList`, { params: params });
};

export const getByID_Base_EquipmentCategory = params => {
    return axios.get(`${base}/api/Base_EquipmentCategory/Get`, { params: params });
};

export const edit_Base_EquipmentCategory = params => {
    return axios.put(`${base}/api/Base_EquipmentCategory/Update`, params);
};

export const add_Base_EquipmentCategory = params => {
    return axios.post(`${base}/api/Base_EquipmentCategory/Add`, params);
};


export const batchRemove_Base_EquipmentCategory = params => {
    return axios.delete(`${base}/api/Base_EquipmentCategory/BatchDelete`, { params: params });
};

export const Remove_Base_EquipmentCategory = params => {
    return axios.delete(`${base}/api/Base_EquipmentCategory/delete`, { params: params });
};



// 设备表
export const getPage_Base_Equipment = params => {
    return axios.get(`${base}/api/Base_Equipment/GetPageList`, { params: params });
};

export const getByID_Base_Equipment = params => {
    return axios.get(`${base}/api/Base_Equipment/Get`, { params: params });
};

export const edit_Base_Equipment = params => {
    return axios.put(`${base}/api/Base_Equipment/Update`, params);
};

export const add_Base_Equipment = params => {
    return axios.post(`${base}/api/Base_Equipment/Add`, params);
};


export const batchRemove_Base_Equipment = params => {
    return axios.delete(`${base}/api/Base_Equipment/BatchDelete`, { params: params });
};

export const Remove_Base_Equipment = params => {
    return axios.delete(`${base}/api/Base_Equipment/delete`, { params: params });
};




